package org.neframework.webtest.base;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.tmsps.neframework.jpa.annotation.DataSource;
import com.tmsps.neframework.jpa.base.DatabaseInfo;

/**
 * 注入数据库连接池
 * 
 * @author Administrator
 *
 */
public class DataSrourceProvider {

	private static DruidDataSource dataSource = null;

	@DataSource(showSql = true)
	public static synchronized DruidDataSource getDataSource() {
		if (dataSource == null) {
			init();
		}
		return dataSource;
	}

	private static void init() {
		Properties p = new Properties();
		InputStream in = DatabaseInfo.class.getClassLoader()
				.getResourceAsStream("druid_derby.properties");
		try {
			p.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			dataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(p);
			dataSource.init();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	public static void main(String[] args) {
		System.err.println(getDataSource().getDriverClassName());
	}

}
